package com.evolved.automata.android.bots.speechbot;
import android.view.View;
import android.widget.*;
import android.app.*;
import android.content.*;
import android.content.res.*;

public class ExpandedOptionsDialog extends Dialog
{
	

	int dId;
	EditText acKeyTextToUpdate;
	EditText acValueTextToUpdate;
	
	
	AutoCompleteTextView expandedKeyText;
	AutoCompleteTextView expandedValueText;
	
	InferenceController model;
	InferenceApp context;
	boolean readOnlyKeysP=false;
	
	public ExpandedOptionsDialog(Context context, InferenceController model)
	{
		super(context);
		this.context=(InferenceApp)context;
		setContentView(R.layout.expanded_options_panel);
		expandedKeyText = (AutoCompleteTextView)findViewById(R.id.expandedKeyId);
		expandedKeyText.setHint("key name");
		
		expandedValueText  = (AutoCompleteTextView)findViewById(R.id.expandedValueId);
		expandedValueText.setHint("key value");
		Resources res = context.getResources();
		//expandedValueText.setLines(res.getInteger(R.integer.expanded_dialog_value_lines));
		this.model=model;
		prepareDialog();
	}
	
	public void setViewsToUpdate(EditText keyView, EditText valueView,boolean readOnlyKeysP)
	{
		acKeyTextToUpdate=keyView;
		acValueTextToUpdate = valueView;
		expandedKeyText.setText(acKeyTextToUpdate.getText());
		expandedValueText.setText(acValueTextToUpdate.getText());
		this.readOnlyKeysP=readOnlyKeysP;
	}
	

	
	private void prepareDialog()
	{
		Button okayButton = (Button)findViewById(R.id.okExpandedButton);
		Button cancelButton = (Button)findViewById(R.id.cancelExpandedButton);
		
		okayButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AutoCompleteTextView keyText  = (AutoCompleteTextView)findViewById(R.id.expandedKeyId);
				AutoCompleteTextView valueText = (AutoCompleteTextView)findViewById(R.id.expandedValueId);;
				acValueTextToUpdate.setText(valueText.getText());
				if (!readOnlyKeysP)
					acKeyTextToUpdate.setText(keyText.getText());
				CheckBox sortBox = (CheckBox)findViewById(R.id.sortTextBox);
				if (sortBox.isChecked())
				{
					RadioButton asc = (RadioButton)findViewById(R.id.ascRadioButton);
					RadioButton desc = (RadioButton)findViewById(R.id.descRadioButton);
					boolean updated;
					updated=context.addSortElement(keyText.getText().toString(), asc.isChecked());
					if (updated)
					{
						context.UpdateSort();
					}
				}
				else
					context.removeSortElement(keyText.getText().toString(), false);
				sortBox.setChecked(false);
				
				CheckBox speechBox = (CheckBox)findViewById(R.id.speakResultCheckBox);
				if (speechBox.isChecked())
				{
					String vText = valueText.getText().toString();
					if (vText.length()>0)
					{
						context.speechController.updateMessages(new String[]{vText});
						context.showDialog(InferenceApp.SPEECH_QUEUE_DIALOG);
					}
				}
				dismiss();
			}
		});
		
		cancelButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				dismiss();
			}
		});
		
		
		expandedKeyText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus==true)
				{
					String[] possbilities = model.retrieveAllKeyNames();
					if (possbilities.length>0)
					{
						ArrayAdapter<String> keyAdapter = new ArrayAdapter<String>(context, R.layout.autocomplete_textview,possbilities);
						expandedKeyText.setAdapter(keyAdapter);
					}
				}
				
			}
		});
		
		expandedValueText.setOnFocusChangeListener(new View.OnFocusChangeListener() 
		{
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) 
			{
				if (hasFocus==false)
				{
					String keyName=expandedKeyText.getText().toString();
					String[] valuePossibilites = model.retrieveValuesForKey(keyName);
					if (valuePossibilites.length>0)
					{
						ArrayAdapter<String> valueAdapter = new ArrayAdapter<String>(context, R.layout.autocomplete_textview,valuePossibilites);
						expandedValueText.setAdapter(valueAdapter);
					}
				}
				
			}
		});
	}
}
